package com.rongc.client.freight.business.supply.view.activity;

import android.view.View;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.gson.GsonHelper;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.business.supply.model.SupplyBean;
import com.rongc.client.freight.business.supply.model.SupplyCarBean;
import com.rongc.client.freight.business.supply.request.api.SupplyFindDriverApi;
import com.rongc.client.freight.business.supply.view.fragment.SupplyConveyanceFragment;
import com.rongc.client.freight.utils.UtilMethod;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Bruce on 2017/7/7.
 */
public class SupplyWaybillActivity extends BaseActivity implements View.OnClickListener{
    SupplyConveyanceFragment fragment;
    String goodsid;
    SupplyBean supplyBean;
    List<SupplyCarBean> supplyCarBeanList;

    @Bind(R.id.tv_departure)
    TextView mTvDeparture;
    @Bind(R.id.tv_total)
    TextView mTvTotal;
    @Bind(R.id.tv_date)
    TextView mTvDate;
    @Bind(R.id.tv_model)
    TextView mTvModel;

    @OnClick({R.id.tv_jia,R.id.tv_canel})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_jia:
                ActivityUtils.toast("加价");
                break;
            case R.id.tv_canel:
                ActivityUtils.toast("取消派单");
                break;
        }
    }

    Response.Listener<JSONObject> respWorkersListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {
                try {
                    supplyBean = GsonHelper.getDeserializer().fromJson(response.getString("goodInfo"), SupplyBean.class);
                    SupplyCarBean[] atemp = GsonHelper.getDeserializer().fromJson(response.getJSONObject("driverInfos").getString("list"), SupplyCarBean[].class);
                    for (int i = 0;i < atemp.length ;i++){
                        supplyCarBeanList.add(atemp[i]);
                    }
                    fragment.setDatas(supplyCarBeanList);
                    makeTopView(supplyBean);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                UtilMethod.dismissProgressDialog(SupplyWaybillActivity.this);
            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            UtilMethod.dismissProgressDialog(SupplyWaybillActivity.this);
        }
    };

    void makeTopView(SupplyBean supplyBean){
        mTvDeparture.setText(supplyBean.getStartPlace() + " - " +supplyBean.getEndPlace());
        mTvTotal.setText(supplyBean.getFreight());
        mTvDate.setText(supplyBean.getLoadTime());
        mTvModel.setText(supplyBean.getCarModels());
    }

    @Override
    public void initParams() {
        goodsid = getIntent().getStringExtra("data");
    }

    @Override
    public void initView() {

        initToolbar(R.string.supply_waybill_title);
        fragment = SupplyConveyanceFragment.getInstance();
        ActivityUtils.addFragmentToActivity(getSupportFragmentManager(),fragment,R.id.contentFrame);
        supplyCarBeanList = new ArrayList<>();

        SupplyFindDriverApi p = new SupplyFindDriverApi(new SupplyFindDriverApi.SupplyFindDriverParams(goodsid),
                respWorkersListener, errorListener);
        RequestManager.getInstance().call(p);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_supply_waybill;
    }
}
